package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.activity.VideoCardDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.VideoCard;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class SocietyDynamicFragment extends BaseListFragment<SocietyDynamicFragment.SocietyDynamicViewHolder, VideoCard> {
    //社团id
    private Long sid;
    public static SocietyDynamicFragment fragment;

    public static SocietyDynamicFragment create(Long sid) {
        SocietyDynamicFragment fragment = new SocietyDynamicFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("sid", sid);
        fragment.setArguments(bundle);
        return fragment;
    }

    public void forwardDetail(Long cardid) {
        Intent intent = new Intent(getActivity(), VideoCardDetailActivity.class);
        intent.putExtra("cardid", intent);
        startActivity(intent);
    }

    public interface SocietyDynamicItemListener {

        public void dynamicClick(VideoCard video);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sid = getArguments().getLong("sid");
    }

    public SocietyDynamicFragment() {
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        return null;
    }

    @Override
    protected SocietyDynamicViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_society_dynamic_item, parent, false);
        SocietyDynamicViewHolder viewHolder = new SocietyDynamicViewHolder(view, new SocietyDynamicItemListener() {
            @Override
            public void dynamicClick(VideoCard video) {
                forwardDetail(video.cardid);
            }
        });
        return viewHolder;
    }

    @Override
    protected SocietyDynamicViewHolder createfootViewHolder(View view) {
        return new SocietyDynamicViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<VideoCard> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_user_follow_list, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }


    public static class SocietyDynamicViewHolder extends BaseRecycleViewHolder<VideoCard> {

        //视频封面
        private NetworkImageView mThum;
        //用户昵称
        private TextView mNickname;
        //视频描述
        private TextView mVideoDesc;
        //发布时间
        private TextView mPubtime;
        //监听
        private SocietyDynamicItemListener listener;


        public SocietyDynamicViewHolder(View itemView, SocietyDynamicItemListener listener) {
            this(itemView);
            this.listener = listener;
        }

        public SocietyDynamicViewHolder(View itemView) {
            super(itemView);
            mThum = (NetworkImageView) itemView.findViewById(R.id.video_thum);
            mNickname = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            mVideoDesc = (TextView) itemView.findViewById(R.id.tv_video_desc);
            mPubtime = (TextView) itemView.findViewById(R.id.tv_pub_time);
        }

        @Override
        public void renderView(VideoCard data, int position) {
            mThum.setImageUrl(data.video.thumUrl, HttpUtils.getImageLoader());
            mNickname.setText(data.user.nickname);
            mVideoDesc.setText(data.description);
            mPubtime.setText(DateUtil.getInterval(data.pubtime));
        }
    }

}
